package org.chen.listener;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.chen.common.model.travel.StoreTravelOrder;
import org.chen.service.AsyncTaskService;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class OrderAgreeRefundConsumer {
    private final ObjectMapper objectMapper;

    private final AsyncTaskService asyncTaskService;

    public OrderAgreeRefundConsumer(ObjectMapper objectMapper,AsyncTaskService asyncTaskService) {
        this.objectMapper = objectMapper;
        this.asyncTaskService = asyncTaskService;
    }

    @KafkaListener(topics = "order-agree-refund", groupId = "order-agree-refunding-group")
    public void listen(String message) {
        try {
            StoreTravelOrder order = objectMapper.readValue(message, StoreTravelOrder.class);
            asyncTaskService.processRefund(order);
        } catch (Exception e) {
            // 处理异常，例如记录日志
            log.error("Error processing order message", e);
        }
    }
}
